package ar.Location.Weather_2;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class LocalWeatherHandler extends DefaultHandler {
	//private static final String TAG = "LocalWeatherHandler";

	boolean current_observation = false;
	boolean display_location = false;
	boolean simpleforecast = false;
	boolean forecastdays = false;
	boolean forecastday = false;
	boolean high = false;
	boolean low = false;

	LocalWeather localWeather = null;
	ForecastDay forecastDay = null;
	
	StringBuffer stringBuffer = new StringBuffer();

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		localWeather = new LocalWeather();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub

		stringBuffer.setLength(0);

		if (localName.equals("current_observation")) {
			current_observation = true;
		}
		if (localName.equals("display_location")) {
			if(current_observation) {
				display_location = true;
			}
		}
		if (localName.equals("simpleforecast")) {
			simpleforecast = true;
		}
		if (localName.equals("forecastdays")) {
			if(simpleforecast) {
				forecastdays = true;
			}
		}
		if (localName.equals("forecastday")) {
			if(simpleforecast && forecastdays) {
				forecastday = true;
				forecastDay = new ForecastDay();
			}
		}
		
		if (localName.equals("high")) {
			if(forecastday) {
				high = true;
			}
		}
		if (localName.equals("low")) {
			if(forecastday) {
				low = true;
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		stringBuffer.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		String innerText = stringBuffer.toString().trim();
		
		if(current_observation) {
			if(display_location) {
				if(localName.equals("full")) {
					localWeather.currentObservation.fullName = innerText;
				}
				if(localName.equals("elevation")) {
					localWeather.currentObservation.elevation = innerText;
				}
			}
			if(localName.equals("observation_time")) {
				localWeather.currentObservation.observationTime = innerText;
			}
			if(localName.equals("weather")) {
				localWeather.currentObservation.weather = innerText;
			}
			if(localName.equals("temp_f")) {
				localWeather.currentObservation.tempF = innerText;
			}
			if(localName.equals("temp_c")) {
				localWeather.currentObservation.tempC = innerText;
			}
			if(localName.equals("relative_humidity")) {
				localWeather.currentObservation.relativeHumidity = innerText;
			}
			if(localName.equals("wind_string")) {
				localWeather.currentObservation.windString = innerText;
			}
			if(localName.equals("pressure_mb")) {
				localWeather.currentObservation.pressureMb = innerText;
			}
			if(localName.equals("pressure_in")) {
				localWeather.currentObservation.pressureIn = innerText;
			}
			if(localName.equals("dewpoint_f")) {
				localWeather.currentObservation.dewPointF = innerText;
			}
			if(localName.equals("dewpoint_c")) {
				localWeather.currentObservation.dewPointC = innerText;
			}
			if(localName.equals("visibility_mi")) {
				localWeather.currentObservation.visibilityMi = innerText;
			}
			if(localName.equals("visibility_km")) {
				localWeather.currentObservation.visibilityKm = innerText;
			}
			if(localName.equals("forecast_url")) {
				localWeather.currentObservation.forecastUrl = innerText;
			}
		}
		
		if(simpleforecast) {
			if(forecastdays) {
				if(forecastday) {
					if(localName.equals("weekday")) {
						forecastDay.dayOfWeek = innerText;
					}
					if(localName.equals("fahrenheit")) {
						if(high) {
							forecastDay.highTempF = innerText;
						}
						if(low) {
							forecastDay.lowTempF = innerText;
						}
					}
					if(localName.equals("celsius")) {
						if(high) {
							forecastDay.highTempC = innerText;
						}
						if(low) {
							forecastDay.lowTempC = innerText;
						}
					}
					if(localName.equals("conditions")) {
						forecastDay.conditions = innerText;
					}
				}
			}
		}
		
		if (localName.equals("current_observation")) {
			current_observation = false;
		}
		if (localName.equals("display_location")) {
			display_location = false;
		}
		if (localName.equals("simpleforecast")) {
			simpleforecast = false;
		}
		if (localName.equals("forecastdays")) {
			if(simpleforecast) {
				forecastdays = false;
			}
		}
		if (localName.equals("high")) {
			if(forecastday) {
				high = false;
			}
		}
		if (localName.equals("low")) {
			if(forecastday) {
				low = false;
			}
		}
		if (localName.equals("forecastday")) {
			if(forecastdays) {
				forecastday = false;
				if(forecastDay != null) {
					localWeather.listForecastDays.add(forecastDay);
				}
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	public LocalWeather getLocalWeather() {
		if (localWeather.listForecastDays != null) {
			return localWeather;
		}
		return null;
	}
}
